package cn.mysite.mycompany.controller;


import cn.mysite.mycompany.entity.User;
import cn.mysite.mycompany.service.CartService;
import cn.mysite.mycompany.service.impl.ProductCategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  ChenChongFei
 * @since 2021-03-13
 */
@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryServiceImpl productCategoryService;

    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    public ModelAndView list(HttpSession session){

        ModelAndView mv=new ModelAndView();
        mv.setViewName("main");
        mv.addObject("list",productCategoryService.getAllProductCategoryVo());
        //用户登陆时获取用户id返回购物车信息
        User user=(User)session.getAttribute("user");
        if(user!=null)
            mv.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        else
            mv.addObject("cartList",new ArrayList<>());


        return mv;
    }

}

